package org.nhindirect.stagent;

import java.rmi.dgc.VMID;
import java.util.ArrayList;

import javax.mail.internet.ContentType;
import javax.mail.internet.ParseException;

public class MultipartEntity extends ArrayList<MimeEntity>
{
    ContentType m_contentType;

    public MultipartEntity()
    {
    	this(Protocol.MultiPartType_Mixed);
    }
    
    public MultipartEntity(String contentType) throws IllegalArgumentException
    {
    	try
    	{
    		this.setContentType(new ContentType(contentType));
    	}
    	catch (ParseException e){}
    }
    
    public MultipartEntity(ContentType contentType)
    {
        this.setContentType(contentType);
    }
    
    public ContentType getContentType()
    {
        return this.m_contentType;
    }
    
    public void setContentType(ContentType value)
    {
        if (value == null)
        {
            throw new IllegalArgumentException();
        }
        
        this.m_contentType = value;
        this.ensureBoundary();     
    }
    
    public String getBoundary()
    {
        return this.m_contentType.getParameter("boundary");
    }

    public MimeEntity toEntity()
    {
        MimeEntity entity = new MimeEntity();
        entity.setParts(this, this.m_contentType.toString());
        return entity;
    }
    
    void ensureBoundary()
    {
    	
        if (this.getBoundary() == null || this.getBoundary().length() == 0)
        {
            this.m_contentType.setParameter("boundary", new VMID().toString());
        }
    }

}
